/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package acceso.login;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 *
 * @author john
 */
public class MenuUtil {

    public static List procesarXmlMenu(Document xml) {
        List menus;
        
        Document doc = xml;

        doc.getDocumentElement().normalize();
        Element mmenu = doc.getDocumentElement();
        
        menus = getFromMenu(mmenu);

        return menus;
    }
    
    public static Document convertirXmlDocument(String xml){
        Document DOCUMENT;
        try {
            DocumentBuilderFactory FACTORY = DocumentBuilderFactory.newInstance();
            DocumentBuilder BUILDER = FACTORY.newDocumentBuilder(); 
            DOCUMENT = BUILDER.parse(new InputSource(new StringReader(xml)));
            procesarXmlMenuMovil(DOCUMENT);
        } catch (Exception ex) {
            DOCUMENT = null;
        }
        return DOCUMENT;
    }
    
    public static Document procesarXmlMenuMovil(Document xml) {        
        Document doc = xml;
        agregarId(doc);
        return doc;
    }
    
    public static List getFirstChilds(Element menu){
        List menus = new ArrayList();
        NodeList hijos = menu.getChildNodes();
        for (int i = 0; i < hijos.getLength(); i++) {
            Node node = hijos.item(i);
            if (node.getNodeName().contentEquals("item")) {
                NamedNodeMap attributes = node.getAttributes();
                String descripcion = (attributes.getNamedItem("titulo") != null)?attributes.getNamedItem("titulo").getNodeValue():"";
                String url = (attributes.getNamedItem("url") != null)?attributes.getNamedItem("url").getNodeValue():"";
                
                Item item = new Item();
                item.setDescripcion(descripcion);
                item.setUrl(url);
                menus.add(item);
            } else if (node.getNodeName().contentEquals("menu")) {
                NamedNodeMap attributes = node.getAttributes();
                String descripcion = (attributes.getNamedItem("titulo") != null)?attributes.getNamedItem("titulo").getNodeValue():"";
                int id = (attributes.getNamedItem("id") != null)?Integer.parseInt(attributes.getNamedItem("id").getNodeValue()):0;
                
                Menu m = new Menu();
                m.setDescripcion(descripcion);
                m.setId(id);
                menus.add(m);
            } else if (node.getNodeName().contentEquals("menu2")) {
                NamedNodeMap attributes = node.getAttributes();
                String descripcion = (attributes.getNamedItem("titulo") != null)?attributes.getNamedItem("titulo").getNodeValue():"";
                int id = (attributes.getNamedItem("id") != null)?Integer.parseInt(attributes.getNamedItem("id").getNodeValue()):0;
                
                Menu m = new Menu();
                m.setDescripcion(descripcion);
                m.setId(id);
                menus.add(m);
            } 
        }
        
        return menus;
    }
    
    private static List getFromMenu(Node menu){
        List menus = new ArrayList();
        NodeList hijos = menu.getChildNodes();

        for (int i = 0; i < hijos.getLength(); i++) {
            Node node = hijos.item(i);
            switch (node.getNodeName()) {
                case "item":
                    {
                        NamedNodeMap attributes = node.getAttributes();
                        String descripcion = (attributes.getNamedItem("titulo") != null)?attributes.getNamedItem("titulo").getNodeValue():"";
                        String url = (attributes.getNamedItem("url") != null)?attributes.getNamedItem("url").getNodeValue():"";
                        String target = (attributes.getNamedItem("target") != null)?attributes.getNamedItem("target").getNodeValue():"";
                        Item item = new Item();
                        item.setDescripcion(descripcion);
                        item.setUrl(url);
                        item.setTarget(target);
                        menus.add(item);
                        break;
                    }
                case "menu":
                    {
                        NamedNodeMap attributes = node.getAttributes();
                        String descripcion = (attributes.getNamedItem("titulo") != null)?attributes.getNamedItem("titulo").getNodeValue():"";
                        Menu m = new Menu();
                        m.setDescripcion(descripcion);
                        m.setMenus(getFromMenu(node));
                        menus.add(m);
                        break;
                    }
                case "menu2":
                    {
                        NamedNodeMap attributes = node.getAttributes();
                        String descripcion = (attributes.getNamedItem("titulo") != null)?attributes.getNamedItem("titulo").getNodeValue():"";
                        Menu2 m2 = new Menu2();
                        m2.setDescripcion(descripcion);
                        m2.setMenus(getFromMenu(node));
                        menus.add(m2);
                        break;
                    }
            }
        }
        
        return menus;
    }

    public static void main(String[] args) {
        DocumentBuilderFactory FACTORY = null;
        DocumentBuilder BUILDER = null;
        Document DOCUMENT = null;
        try {
            FACTORY = DocumentBuilderFactory.newInstance();
            BUILDER = FACTORY.newDocumentBuilder();
            DOCUMENT = BUILDER.parse(new File("/home/edsonsuarez/Documentos/menu.xml"));

            List m = procesarXmlMenu(DOCUMENT);
            for(int i = 0; i<m.size(); i++){
                System.out.println(""+m.get(i));
            }

        } catch (Exception ep) {
            System.out.println(">" + ep.getMessage());
        }
    }

    private static void agregarId(Document doc) {
        Element mmenu = doc.getDocumentElement();
        NodeList menus = mmenu.getElementsByTagName("menu");
        int contador = 10;
        for(int i=0; i<menus.getLength(); i++){
            Element menu = (Element) menus.item(i);
            menu.setAttribute("id", contador+"");
            contador++;
        }
    }

}